<?php

namespace app\api\controller;
use think\Db;
class Building
{
//    public function tj(){
//        $i = 31;
//        $z = 0;
////        $row = [];
//        for ($j=1;$j<=34;$j++){
//
//            for ($k=1;$k<=8;$k++){
//
//                $row = [
//                    'building'=>$i,
//                    'room'=>$j.'0'.$k
//                ];
//                $z = $z+1;
//                    Db::table('tbl_khhy')->insert($row);
//            }
//
//        }
//        dump($z);
//    }
//北11-25，中1-10，南26-33
    public function index(){
        header("Access-Control-Allow-Origin: *");
        $building = Db::table('tbl_khhy')->field('building,count(1) as total')->group('building')->select();
        $check = Db::table('tbl_khhy')->field('building,count(1) as badge')->where('status',1)
            ->group('building')->column('count(1)','building');
        foreach ($building as $k=>$v){
            $building[$k]['badge'] = $check[$v['building']]??0;
            $building[$k]['rate'] = round($building[$k]['badge']/$v['total'],3)*100 . '%';
            $b = $v['building'];
            if ($b<=10){
                $building[$k]['text'] = "中区{$b}栋";
            }elseif ($b<=25){
                $building[$k]['text'] = "北区{$b}栋";
            }else{
                $building[$k]['text'] = "南区{$b}栋";
            }
            $r = round($building[$k]['badge']/$v['total'],2);
            if ($r<0.6){
                $building[$k]['type'] = "error";
            }elseif ($r<0.8){
                $building[$k]['type'] = "warning";
            }else{
                $building[$k]['type'] = "success";
            }
        }
        echo json_encode($building);
    }

    public function badge(){
        header("Access-Control-Allow-Origin: *");
        $b = input('building');
        $rooms =Db::table('tbl_khhy')->where('building',$b)->order('room')->select();
        $floor = Db::table('tbl_khhy')->where('building',$b)->max('floor');
        $status = Db::table('tbl_khhy')->where('building',$b)->sum('status');

        $unbadge = count($rooms) - $status;
        if ($b<=10){
            $title = "中区{$b}栋";
        }elseif ($b<=25){
            $title = "北区{$b}栋";
        }else{
            $title = "南区{$b}栋";
        }
        $remark = "总户数:".count($rooms).": 已绑定{$status}户 未绑定{$unbadge}户";
        foreach ($rooms as $k=>$v){
            $rooms[$k]['inverted'] = $v['status']?false:true;
        }
        echo json_encode(['title'=>$title,'remark'=>$remark,'building'=>$b,'floor'=>$floor,'rooms'=>$rooms]);

    }

    public function stat(){
        header("Access-Control-Allow-Origin: *");
        $b = input('bb');
        $r = input('rr');
        $s = input('ss');
        Db::table('tbl_khhy')->where('building',$b)->where('room',$r)->setField('status',$s);

    }
    public function showTotal(){
        header("Access-Control-Allow-Origin: *");

        $total = Db::table('tbl_khhy')->count();
        $badge = Db::table('tbl_khhy')->where('status',1)->count();
        $unbadge = $total - $badge;
        $rate = round($badge/$total,3)*100;

        $msg = "总户数:{$total} 已绑定:{$badge} 未绑定:{$unbadge} 绑定率:{$rate}%";
        echo json_encode(['result'=>1,'msg'=>$msg]);
    }

    public function backup(){
        $sql = "insert into tbl_khhy_back (building,floor,room,`status`,create_at) SELECT building,floor,room,`status`,NOW() from tbl_khhy";
        Db::query($sql);
    }
}